/**
 * @author SilverBiology, LLC
 * @website http://code.google.com/p/gbif-spreadsheet-processor/
*/
Ext.namespace('GBIF');
Ext.namespace('GBIF.GeoSpatial')

GBIF.GeoSpatial.KML = function(config){
	
	Ext.apply(this,config,{	
			border: false
		,	tbar: [{
					xtype:'label'
				,	style:"padding:0 5px"	
				,	text:'Enter KML feed:'	
			},{
					xtype: 'textfield'  
				,	width: 350
				,	ref: '../url'
				,	scope:this
				,	vtype:'url'
				, enableKeyEvents: true 
				, listeners: {
						keypress:{
								scope:this
							, fn: function( textfield, e ) {
									if (e.getKey() == e.ENTER) {
										this.loadKML();
									}
								}
						}
					}	
			},{
					text: 'Load'
				,	style:"padding:5px"
				, scope: this
				,	handler: this.loadKML
			}]
		,	proxyUrl: "../resources/api/getcapabilities-proxy.php?url="		
	});
	
	GBIF.GeoSpatial.KML.superclass.constructor.call(this, config);
}

Ext.extend(GBIF.GeoSpatial.KML,GBIF.GeoSpatial.MapComponent, {
	
	loadKML: function(){
		this.url.setValue(this.url.getValue().trim());
		if(!(this.url.getValue()=='')){
			if (this.url.isValid()) {
				var urlValue = this.proxyUrl + this.url.getValue();
				var layer = new OpenLayers.Layer.Vector("KML", {
            strategies: [new OpenLayers.Strategy.Fixed()],
            protocol: new OpenLayers.Protocol.HTTP({
                url: urlValue,
                format: new OpenLayers.Format.KML({
										extractStyles: true
									,	extractAttributes: true
                })
            })
        })				
				this.map.addLayer(layer);	
			} else {
				GBIF.GeoSpatial.Notice.msg(this.txtError,"Please enter valid URL.");
			}	
		} else {
			Ext.MessageBox.alert('error','Please enter KML URL');
		}
	}
});